﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Nest;

namespace CompanySearch.Library.EleasticSearch
{
    public class ESRepository : CompanySearch.Library.Repository.IRepository
    {
        ElasticClient _client;

        public ESRepository(string esUri)
        {
            var uri = new Uri(esUri);
            var settings = new ConnectionSettings(uri, defaultIndex: "dsrsystem");

            this._client = new ElasticClient(settings);
        }



        public List<Mongo.Company> GetCompanyByStartsWith(string text, int skip, int take)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return new List<Mongo.Company>();
            }
            var textToMatch = string.Format("\"{0}\"", text.Replace("\"", "\\\""));

            var startsWith = string.Format("{0}.*", textToMatch);
            


            var query1 = this._client.Search<Company>(s => s
                .Index("dsrsystem")
                .SortAscending(sort => sort.Name)
                .Query(q =>
                    q.Regexp(
                        (f) => f.Value(startsWith).OnField(onF => onF.Name)))
                .Skip(skip)
                .Take(take)
                );

            var results = from c in query1.Documents
                          select new Mongo.Company { Id = c.Id, Name = c.Name };

            return results.ToList();


        }

        public List<Mongo.Company> GetCompanyByContains(string text,int skip, int take)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return new List<Mongo.Company>();
            }
            var textToMatch = string.Format("\"{0}\"", text.Replace("\"", "\\\""));

            var startsWith = string.Format("{0}.*", textToMatch);
            var containsNotStartWith = string.Format("~({0}.*)&(.*{1}.*)", textToMatch, textToMatch);
            var query2 = this._client.Search<Company>(s => s
             .Index("dsrsystem")
             .SortAscending(sort => sort.Name)
             .Query(q =>
                 q.Regexp(
                     (f) => f.Value(containsNotStartWith).OnField(onF => onF.Name)))
              .Skip(skip)
              .Take(take));

            var results = from c in query2.Documents
                          select new Mongo.Company { Id = c.Id, Name = c.Name };

            return results.ToList();
            
        }

        public List<Guid> GetCompaniesByAttribute(string textToMatch)
        {
            return new List<Guid>();
        }


        public List<Mongo.Company> GetCompaniesByIds(List<Guid> ids)
        {
            return new List<Mongo.Company>();
        }
    }



    public class Company
    {
        public Guid Id { get; set; }

        [ElasticProperty(OmitNorms = true, Index = FieldIndexOption.NotAnalyzed, AddSortField = true)]
        public string Name { get; set; }


        [ElasticProperty(OmitNorms = true, Index = FieldIndexOption.Analyzed, AddSortField = false)]
        public string WordifiedName { get; set; }
    }
    
}
